package final_wearebatman_model;

import final_wearebatman_view.MessageBox;

//kompozit elem megvalósítása
public class Composite extends Circuit implements IhasInput, IhasOutput {

    private Input[] inputs; //több inputja lehet
    private Output[] outputs; //több outputja lehet
    private Wire[] wireInputs;
    private Wire[] wireOutputs;
    public int counter; //rc számláló
    protected int prevInputs[];
    private String name; //név
    private int in, out;
    private int RCcount;

    //konstruktor
    public Composite(String name, String filename) {
        super(filename, null);

        int outcounter = 0;
        for (int i = 0; i < this.table.size(); i++) {
            if (this.table.containsKey("out" + (i + 1))) {
                outcounter++;
            }
        }

        out = outcounter;
        in = this.sourcelist.size();

        this.outputs = new Output[out];
        for (int i = 0; i < out; i++) {
            this.outputs[i] = new Output();
        }
        this.inputs = new Input[in];
        for (int i = 0; i < in; i++) {
            this.inputs[i] = new Input();
        }
        this.wireOutputs = new Wire[out];
        this.wireInputs = new Wire[in];
        this.counter = 0;
        this.prevInputs = new int[in];
        this.startedSimulation = true;
        this.name = name;
    }

    //inputok számának lekérése
    public int getInputNumber() {
        return this.in;
    }

    //ouputok számának lekérése
    public int getOutputNumber() {
        return this.out;
    }

    //a kompozit elem működését implementáló metódus
    public void operate() {
        if (this.counter > this.inputs.length) {
            for (int i = 0; i < prevInputs.length; i++) {
                if ((this.prevInputs[i] != this.inputs[i].getData())) {
                    RCcount++;
                    if (RCcount > 1) {
                        MessageBox b = new MessageBox(this.name);
                        return;
                    }
                }
            }
        }
        
        for (int i = 0; i < inputs.length; i++) {
            String s = table.get("in" + (i + 1)).toString();
            Switch sw = (Switch) this.sourcelist.get(Integer.parseInt(s.substring(1).toString()));
            sw.setState(this.inputs[i].getData());
            sw.setOutputData(0,this.inputs[i].getData());
            System.out.println(i+": "+this.inputs[i].getData());
        }

        this.tick(1);

        for (int i = 0; i < outputs.length; i++) {
            String s = table.get("out" + (i + 1)).toString();
            Ioperable item = this.gatelist.get(Integer.parseInt(s.substring(1).toString()));
            this.outputs[i].setData(Integer.parseInt(item.getFinalState()));
        }

        this.counter++;

        if (counter < 10) {
            for (int i = 0; i < this.wireOutputs.length; i++) {
                this.wireOutputs[i].operate();
            }
        }
    }

    //setter az inputokra
    public void setInputData(int pin, int data) {
        this.inputs[pin].setData(data);
    }

    //getter az inputokra
    public int getInputData(int pin) {
        return this.inputs[pin].getData();
    }

    //vezetékek inputjának összekötés
    public void connectWireInput(int pin, Wire wire) {
        this.wireInputs[pin] = wire;
    }

    //getter az outputokra
    public int getOutputData(int pin) {
        return this.outputs[pin].getData();
    }

    //setter az outputokra
    public void setOutputData(int pin, int data) {
        this.outputs[pin].setData(data);
    }

    //vezetékek outputjának összekötése
    public void connectWireOutput(int pin, Wire wire) {
        this.wireOutputs[pin] = wire;
    }

    //név lekérése
    public String getName() {
        return this.name;
    }

    //számlálók nullázása
    public void resetCounter() {
        this.RCcount = 0;
        this.counter = 0;
    }

    //operate utáni állapot beállítása
    public String getFinalState() {
        return Integer.toString(this.outputs[0].getData());
    }
}
